var UserAgent = {
    ua : navigator.userAgent,
    getOs : function ()
    {
        var brand = 'Other';
        if(this.ua.indexOf('Windows') != -1) {
            brand = 'Windows';
        } else if(this.ua.indexOf('Linux') != -1) {
            brand = 'Linux';
        }
        var name = 'Other';
        if(this.ua.indexOf('Windows NT 5.1') != -1 || this.ua.indexOf('Windows NT 5.2') != -1) {
            name = 'XP';
        } else if(this.ua.indexOf('Windows NT 6.0') != -1) {
            name = 'Vista';
        } else if(this.ua.indexOf('Windows NT 6.1') != -1) {
            name = '7';
        } else if(this.ua.indexOf('Windows NT 6.2') != -1) {
            name = '8';
        }
        var bits = '32';
        if(this.ua.indexOf('Win64') != -1 || this.ua.indexOf('x64') != -1 || this.ua.indexOf('WOW64') != -1 || this.ua.indexOf('x86_64') != -1) {
            bits = '64';
        }
        return {
            'brand' : brand, //Windows, Linux, OS X
            'name' : name, //XP, Vista, 7
            'bits' : bits //32, 64
        };
    },
    getUserAgent : function()
    {
        var name = 'Other';
        var version = '0';
        if(this.ua.indexOf('LBBROWSER') != -1) {
            name = 'LBBROWSER';
            if(window.external && window.external.LiebaoGetVersion) {
                version = window.external.LiebaoGetVersion();
            }
        } else if(this.ua.indexOf('TaoBrowser') != -1) {
            name = 'TaoBrowser';
            var pos = this.ua.indexOf('TaoBrowser');
            var tmp = this.ua.substring(pos);
            pos = tmp.indexOf(' ');
            version = tmp.substring(11, pos);
        } else if(this.ua.indexOf('Chrome') != -1) {
            name = 'Chrome';
            var pos = this.ua.indexOf('Chrome');
            var tmp = this.ua.substring(pos);
            pos = tmp.indexOf(' ');
            version = tmp.substring(7, pos);
        } else if(this.ua.indexOf('Firefox') != -1) {
            name = 'Firefox';
            var pos = this.ua.indexOf('Firefox');
            version = this.ua.substring(pos + 8);
        } else if(this.ua.indexOf('MSIE') != -1) {
            name = 'IE';
            var pos = this.ua.indexOf('MSIE'); //从MSIE截取到分号，因为6.0是3字节，而10.0是4字节，以后更长。
            var tmp = this.ua.substring(pos);
            pos = tmp.indexOf(';');
            version = tmp.substring(5, pos);
        }
        return {
            'name' : name, //LBBROWSER IE Firefox Chrome
            'version' : version
        };
    },
    actionShowUpgradeNotice : function(notice)
    {
        var div = document.createElement("div");
        div.className = 'module_upgrade_browser';
        var p = document.createElement("p");
        p.innerHTML = notice;
        div.appendChild(p);
        document.body.insertBefore(div, document.body.childNodes[0]);
    },
    attachEventShowUpgradeNotice : function()
    {
        var os = this.getOs();
        var ua = this.getUserAgent();
        if(ua.name != 'IE') {
            return true;
        }
        var notice = null;
        switch (ua.version) {
        case '6.0' :
            notice = '你正在使用2001年的IE6';
            break;
        case '7.0' :
            notice = '你正在使用2006年的IE7';
            break;
        case '8.0' :
            notice = '你正在使用2009年的IE8';
            break;
        case '9.0' :
            notice = '你正在使用2011年的IE9';
            break;
        case '10.0' :
            notice = '你正在使用2012年的IE10';
            break;
        }
        switch (os.name) {
        case 'XP' :
            //XP 最高能升级到IE8
            if(ua.version == '6.0' || ua.version == '7.0') {
                this.actionShowUpgradeNotice(notice + '，无法使用本网站的功能，建议升级到<a href="http://windows.microsoft.com/zh-CN/windows/upgrade-your-browser" target="_blank">IE8</a>或 <a href="http://j.union.ijinshan.com/jump.php?u_key=143781" target="_blank">猎豹浏览器</a>。');
            }
            break;
        case 'Vista' :
        case '7' :
            //Vista 7 最高能升级到IE9
            if(ua.version == '7.0' || ua.version == '8.0') {
                this.actionShowUpgradeNotice(notice + '，无法使用本网站的功能，建议升级到<a href="http://windows.microsoft.com/zh-CN/windows/upgrade-your-browser" target="_blank">IE9</a>或 <a href="http://j.union.ijinshan.com/jump.php?u_key=143781" target="_blank">猎豹浏览器</a>。');
            }
            break;
        }
    }
};
